<script>
	var is_connect = false;
	var currentDevice = {};
	function ab2ascii(buffer) {
	  let arr = [];
	  Array.prototype.map.call(new Uint8Array(buffer), function (bit) {
		arr.push("0x" + bit.toString(16));
	  });
	  return arr;
	}
	import { mapMutations } from "vuex"
	export default {
		data(){
			return {
				timerHeartbeat: null
			}
		},
		methods: {
			...mapMutations(["setSearchBluetList"]),
		},
		onLaunch: function() {
			console.log('App Launch')
			/* 小程序更新 */
			if(wx.canIUse('getUpdateManager')) {
				const updateManager = wx.getUpdateManager()
				updateManager.onCheckForUpdate(function (res) {
					if (res.hasUpdate) {
						updateManager.onUpdateReady(function () {
							wx.showModal({
							  title: '更新提示',
							  content: '新版本已经准备好，是否重启应用？',
							  success: function (res) {
								if (res.confirm) {
								  updateManager.applyUpdate()
								}
							  }
							})
						})
						updateManager.onUpdateFailed(function () {
							wx.showModal({
								title: '已经有新版本了哟~',
								content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~'
							})
						})
					}
				})
			}
			uni.$on('onBluetoothDeviceFound', () => {
				wx.onBLECharacteristicValueChange((res) => {
					let arr = ab2ascii(res.value);
					this.$store.dispatch("onBLEread",arr);
				});
				wx.onBluetoothDeviceFound(async res => {
					// console.log("res.devices[0]",res.devices[0]);
					is_connect = true
					let isDevice = (res.devices[0].RSSI >= -80 && res.devices[0].name); // (res.devices[0].name == "XY_BLE_1224" && res.devices[0].RSSI >= -80) //(res.devices[0].name == "MIKAPU(BLE)" && res.devices[0].RSSI >= -80);
					if(!isDevice) return;
					let searchBluetList = JSON.parse(JSON.stringify(this.$store.state.searchBluetList));
					let index = searchBluetList.findIndex(item => item.deviceId === res.devices[0].deviceId);
					if(index == -1) {
						searchBluetList.push(res.devices[0]);
						// this.setSearchBluetList(searchBluetList);
						this.$store.state.searchBluetList = searchBluetList;
					}
				});
			});
			uni.onBLEConnectionStateChange((res) => {
				is_connect = res.connected;
				console.log("is_connect",is_connect,this.$store.state.currentDevice,currentDevice)
				if(res.connected) {
					this.$store.dispatch("stopSearchBlue",() => {
						if(!this.timerHeartbeat){
							this.timerHeartbeat = setInterval(() => {
								this.$store.dispatch("currentBlueData",[0x1B, 0x03, 0x00, 0x01, 0x00, 0x62]); 
							},1400);
						};
						uni.navigateTo({
							url: "/pages/index/index"
						})
						currentDevice = {
							deviceId: "",
							serviceId: "",
							characteristicId: ""
						};
					});
				}else{
					clearInterval(this.timerHeartbeat);
					this.timerHeartbeat = null;
					uni.navigateTo({
						url: "/pages/search/index"
					});
					if(Object.keys(currentDevice).length && Object.keys(this.$store.state.currentDevice).length){
						this.$store.state.currentDevice = currentDevice;
						uni.setStorageSync("currentDevice",JSON.stringify(this.$store.state.currentDevice));
					}
				}
			});
		},
		onShow: function() {
			let getSystemInfo = () => {
				let info = wx.getSystemInfoSync()
				if (info.platform == "android" && !info.locationAuthorized) {
					uni.showModal({
						title: "提示",
						content: "请授予微信位置权限(GPS)",
						showCancel: false,
						success: () => {
							uni.closeBluetoothAdapter({
								success: () =>{
									this.$store.dispatch("initBlue", true);
									getSystemInfo()
								}
							});
						}
					})
				  return
				}
				if (info.platform == "android" && !info.locationEnabled) {
					uni.showModal({
						title: "提示",
						content: "请打开位置信息(GPS)",
						showCancel: false,
						success: () => {
							uni.closeBluetoothAdapter({
								success: () =>{
									this.$store.dispatch("initBlue", true);
									getSystemInfo()
								}
							});
						}
					});
				  return
				}
				this.$store.dispatch("initBlue", true);
				if(!is_connect) {
					uni.navigateTo({
						url: "/pages/search/index"
					})
				}
			}
			getSystemInfo();
		},
		onHide: function() {
			console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
</style>
